package HuaRongDong;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Person extends JButton implements FocusListener{
    int number;
    Color c=new Color(255,245,170);
    Font font=new Font("宋体",Font.BOLD,12);

    public Person(int number, String s) {
        super(s);
        setBackground(c);
        setFont(font);
        this.number=number;
        c=getBackground();
        addFocusListener(this);
    }

    public void addKeyListener(HuaRongDao huaRongDao) {
    }
    public void addMouseListener(HuaRongDao huaRongDao) {
    }

    @Override
    public void focusGained(FocusEvent e) {
        setBackground(Color.RED);
    }

    @Override
    public void focusLost(FocusEvent e) {
        setBackground(c);
    }
}

class HuaRongDao extends JFrame implements KeyListener, MouseListener, ActionListener{
    Person person[]=new Person[10];
    JButton above,below,left,right;
    JButton restart=new JButton("重新开始");

    public HuaRongDao() {
       init();
       setVisible(true);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       setBounds(200,200,320,500);
       validate();
    }

    private void init() {
        setLayout(null);
        add(restart);
        restart.setBounds(100,320,120,35);
        restart.addActionListener(this);
        String name []={"曹操","关羽","张飞","刘备","周瑜","黄盖","兵","兵","兵","兵"};
        for (int i = 0; i < name.length; i++) {
            person[i]=new Person(i,name[i]);
            person[i].addKeyListener(this);
            person[i].addMouseListener(this);
            add(person[i]);
        }
        person[0].setBounds(104,54,100,100);
        person[1].setBounds(104,154,100,50);
        person[2].setBounds(54,154,50,100);
        person[3].setBounds(204,154,50,100);
        person[4].setBounds(54,54,50,100);
        person[5].setBounds(204,54,50,100);
        person[6].setBounds(54,254,50,50);
        person[7].setBounds(204,254,50,50);
        person[8].setBounds(104,204,50,50);
        person[9].setBounds(154,204,50,50);
        person[9].requestFocus();
        left=new JButton();
        right=new JButton();
        above=new JButton();
        below=new JButton();
        add(left);
        add(right);
        add(above);
        add(below);
        left.setBounds(49,49,5,260);
        right.setBounds(254,49,5,260);
        above.setBounds(49,49,210,5);
        below.setBounds(49,304,210,5);
        validate();
    }





    @Override
    public void actionPerformed(ActionEvent e) {
        dispose();
        new HuaRongDao();
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        Person man= (Person) e.getSource();
        if (e.getKeyCode()== KeyEvent.VK_DOWN){
            go(man,below);
        }
        if (e.getKeyCode()==KeyEvent.VK_UP){
            go(man,above);
        }
        if (e.getKeyCode()==KeyEvent.VK_LEFT){
            go(man,left);
        }
        if (e.getKeyCode()==KeyEvent.VK_RIGHT){
            go(man,right);
        }
    }

    private void go(Person man, JButton direction) {
        boolean move=true;
        Rectangle manRect=man.getBounds();
        int x=man.getBounds().x;
        int y=man.getBounds().y;
        if (direction==below)
            y+=50;
        if (direction==above)
            y-=50;
        if (direction==left)
            x-=50;
        if (direction==right)
            x+=50;
        manRect.setLocation(x,y);
        Rectangle directionRect=direction.getBounds();
        for (int i = 0; i < 10; i++) {
            Rectangle personRect=person[i].getBounds();
            if (manRect.intersects(personRect)&&(man.number!=i));
              move=false;
        }
        if (manRect.intersects(directionRect))
            move=false;
        if (move=true)
            man.setLocation(x,y);
    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {
        Person man= (Person) e.getSource();
        int x=-1,y=-1;
        x=e.getX();
        y=e.getY();
        int w=man.getBounds().width;
        int h=man.getBounds().height;
        if (y>h/2)
            go(man,below);
        if (y>h/2)
            go(man,above);
        if (x>w/2)
            go(man,left);
        if (x>h/2)
            go(man,right);

    }

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}
}

public class GameMain {
    public static void main(String[] args) {
        HuaRongDao game=new HuaRongDao();
    }
}
